package com.easylinkin.linkappapi.linkage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.linkage.entity.LinkageLog;
import com.easylinkin.linkappapi.linkage.mapper.LinkageLogMapper;
import com.easylinkin.linkappapi.linkage.service.LinkageLogService;
import com.easylinkin.linkappapi.ruleengine.mapper.RuleExecutionMapper;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 * 联动执行日志 服务实现类
 * </p>
 *
 * @author TongJie
 * @since 2020-06-09
 */
@Service
public class LinkageLogServiceImpl extends ServiceImpl<LinkageLogMapper, LinkageLog> implements LinkageLogService {

    @Resource
    private RuleExecutionMapper ruleExecutionMapper;

    @Override
    public IPage<LinkageLog> getLinkageLogPage(Page page, LinkageLog linkageLog) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (linkageLog.getState() != null) {
            queryWrapper.eq("state", linkageLog.getState());
        }
        if (linkageLog.getType() != null) {
            queryWrapper.eq("type", linkageLog.getType());
        }
        if (!ObjectUtils.isEmpty(linkageLog.getDeviceCode())) {
            queryWrapper.eq("device_code", linkageLog.getDeviceCode());
        }
//        if (!ObjectUtils.isEmpty(linkageLog.getLinkageConfigId())) {
//            queryWrapper.eq("linkage_config_id", linkageLog.getLinkageConfigId());
//        }
//
        if (!ObjectUtils.isEmpty(linkageLog.getRuleEngineId())) {
            queryWrapper.eq("rule_engine_id", linkageLog.getRuleEngineId());
        }
        if (!ObjectUtils.isEmpty(linkageLog.getQueryTimeStart())) {
            queryWrapper.ge("save_time", linkageLog.getQueryTimeStart());
        }
        if (!ObjectUtils.isEmpty(linkageLog.getQueryTimeEnd())) {
            queryWrapper.le("save_time", linkageLog.getQueryTimeEnd());
        }
        queryWrapper.orderByDesc("save_time");
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public LinkageLog getLinkageLog(String id) {
        if (ObjectUtils.isEmpty(id)) {
            return null;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        return getById(id);
    }
}
